package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ClueErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupGetConditionDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSeniorDeleteDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSeniorDetailDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSeniorFormListDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSeniorMoveDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSeniorSaveDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSetDeleteDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSetItemDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSetListDTO;
import com.xbongbong.pro.publicgroup.pojo.dto.PublicGroupSetSaveDTO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupFormListVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupGetConditionVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupItemListVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSeniorFormListVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSeniorItemVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSeniorListVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSetDeleteVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSetItemVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSetListVO;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSetSaveVO;
import com.xbongbong.saas.help.PublicSeniorRuleHelp;
import com.xbongbong.saas.service.PublicGroupSeniorService;
import com.xbongbong.saas.service.PublicGroupService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 公海池分组
 * @Author: hongxiao
 * @date: 2019-03-03 14:53
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/publicGroup")
public class PublicGroupController {

    @Resource
    private PublicGroupService publicGroupService;
    @Resource
    private PublicGroupSeniorService publicGroupSeniorService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private PublicSeniorRuleHelp publicSeniorRuleHelp;


    /**
     * 分组列表
     * @param setListDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-03-03 15:18
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/set/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setList(@RequestBody @Valid PublicGroupSetListDTO setListDTO, BindingResult br) {
        XbbResponse<PublicGroupSetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupSetListVO setListVO = publicGroupService.setList(setListDTO);
                response = new XbbResponse<>(setListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 分组列表
     * @param setListDTO 入参
     * @param br
     * @return
     * @throws
     */
    @RequestMapping(value = "/itemList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String itemList(@RequestBody @Valid PublicGroupSetListDTO setListDTO, BindingResult br) {
        XbbResponse<PublicGroupItemListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupItemListVO setListVO = publicGroupService.groupItemList(setListDTO);
                response = new XbbResponse<>(setListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 编辑分组信息
     * @param setItemDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-03-03 15:18
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/set/item", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setItem(@RequestBody @Valid PublicGroupSetItemDTO setItemDTO, BindingResult br) {
        XbbResponse<PublicGroupSetItemVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupSetItemVO setItemVO = publicGroupService.setItem(setItemDTO);
                response = new XbbResponse<>(setItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 保存分组
     * @param setSaveDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-03-03 15:18
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/set/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setSave(@RequestBody @Valid PublicGroupSetSaveDTO setSaveDTO, BindingResult br) {
        XbbResponse<PublicGroupSetSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(setSaveDTO.getCorpid(), setSaveDTO.getBusinessType())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                PublicGroupSetSaveVO setSaveVO = publicGroupService.setSave(setSaveDTO);
                response = new XbbResponse<>(setSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 删除分组
     * @param setDeleteDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-03-03 15:18
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/set/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setDelete(@RequestBody @Valid PublicGroupSetDeleteDTO setDeleteDTO, BindingResult br) {
        XbbResponse<PublicGroupSetDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(setDeleteDTO.getCorpid(), setDeleteDTO.getBusinessType())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                PublicGroupSetDeleteVO setDeleteVO = publicGroupService.setDelete(setDeleteDTO);
                response = new XbbResponse<>(setDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取筛选条件
     * @param getConditionDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-03-03 15:18
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/getCondition", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCondition(@RequestBody @Valid PublicGroupGetConditionDTO getConditionDTO, BindingResult br) {
        XbbResponse<PublicGroupGetConditionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupGetConditionVO getConditionVO = publicGroupService.getCondition(getConditionDTO);
                response = new XbbResponse<>(getConditionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    /**
     * 高级、标准模式下，公海池上方tab列表
     * @param setListDTO 入参
     * @param br
     * @return
     * @throws
     */
    @RequestMapping(value = "/senior/tabList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String seniorTabList(@RequestBody @Valid PublicGroupSetListDTO setListDTO, BindingResult br) {
        XbbResponse<PublicGroupItemListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupItemListVO setListVO = publicGroupService.groupTabList(setListDTO);
                response = new XbbResponse<>(setListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     *公海池高级模式，左侧列表返回
     * @param setListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/set/senior/formList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String seniorFormList(@RequestBody @Valid PublicGroupSeniorFormListDTO setListDTO, BindingResult br) {
        XbbResponse<PublicGroupSeniorFormListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupSeniorFormListVO setListVO = publicGroupSeniorService.getSeniorFormList(setListDTO);
                response = new XbbResponse<>(setListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     *公海池标准模式，左侧列表返回
     * @param setListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/set/formList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formList(@RequestBody @Valid PublicGroupSeniorFormListDTO setListDTO, BindingResult br) {
        XbbResponse<PublicGroupFormListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupFormListVO setListVO = publicGroupSeniorService.getFormList(setListDTO);
                response = new XbbResponse<>(setListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 高级模式，公海分组保存
     * @param setSaveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/set/senior/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String seniorSave(@RequestBody @Valid PublicGroupSeniorSaveDTO setSaveDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(setSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(setSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(setSaveDTO.getCorpid(), setSaveDTO.getBusinessType())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                BaseVO setSaveVO = publicGroupSeniorService.add(setSaveDTO);
                response = new XbbResponse<>(setSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 高级模式下分组删除
     * @param setDeleteDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/set/senior/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setDelete(@RequestBody @Valid PublicGroupSeniorDeleteDTO setDeleteDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(setDeleteDTO.getCorpid(), setDeleteDTO.getBusinessType())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                BaseVO setDeleteVO = publicGroupSeniorService.delete(setDeleteDTO);
                response = new XbbResponse<>(setDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 高级模式下，获得分组规则详情
     * @param setItemDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/set/senior/item", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setItem(@RequestBody @Valid PublicGroupSeniorDetailDTO setItemDTO, BindingResult br) {
        XbbResponse<PublicGroupSeniorItemVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupSeniorItemVO setItemVO = publicGroupSeniorService.getItem(setItemDTO);
                response = new XbbResponse<>(setItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 高级模式下分组移动后保存
     * @param setSaveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/set/senior/move", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String seniorSave(@RequestBody @Valid PublicGroupSeniorMoveDTO setSaveDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(setSaveDTO.getCorpid(), setSaveDTO.getBusinessType())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                BaseVO setSaveVO = publicGroupSeniorService.move(setSaveDTO);
                response = new XbbResponse<>(setSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 高级模式下，模版下分组数返回
     * @param setItemDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/set/senior/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getGroupTree(@RequestBody @Valid PublicGroupSeniorDetailDTO setItemDTO, BindingResult br) {
        XbbResponse<PublicGroupSeniorListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupSeniorListVO setItemVO = publicGroupSeniorService.getByFormId(setItemDTO);
                response = new XbbResponse<>(setItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
